package ru.aptu.gsp_banking.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.aptu.gsp_banking.service.AccountService;
import ru.aptu.gsp_banking.service.CurrencyService;
import ru.aptu.gsp_banking.snmp.Sender;

import java.io.IOException;
import java.security.Principal;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 11/16/11
 * Time: 4:07 PM
 */

@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

    private boolean posted = true;

    @RequestMapping(value = "/accounts", method = RequestMethod.POST)
    public String listAccountsPost() {
        posted = true;
        return "redirect:accounts";
    }

    // accounts is our main page
    @RequestMapping(value = "/accounts", method = RequestMethod.GET)
    public String listAccountsGet(Map<String, Object> map, Principal principal) {
        if (!posted || principal == null) {
            posted = true;
            return "redirect:login.jsp";
        }
        posted = false;

        final String currentUser = principal.getName();
        map.put("accountList", accountService.listAccount(currentUser));

        Sender.sendV2cTrap("Success: user " + currentUser + " requested accounts page.");

        return "accounts";
    }
}